﻿using System;
using System.Linq;
using DynamicLinqExtensions;
using DynamicLinqExtensionsTest.ModelTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DynamicLinqExtensionsTest
{
    [TestClass]
    public class DynamicWhereExtensionsTest
    {
        [TestMethod]
        public void DynamicWhere_PropertyIsString_FiltersCorrectly()
        {
            var people = DataContext.People();
            var person = (Person) people.DynamicWhere("FirstName", "Chris").Single();
            Assert.IsTrue(person.FirstName == "Chris");
        }

        [TestMethod]
        public void DynamicWhere_PropertyIsint_FiltersCorrectly()
        {
            var people = DataContext.People();
            var person = (Person)people.DynamicWhere("Id", 0).Single();
            Assert.IsTrue(person.FirstName == "Chris");
        }

        [TestMethod]
        public void DynamicWhere_PropertyIsIntCriteriaIsString_FiltersCorrectly()
        {
            var people = DataContext.People();
            var person = (Person)people.DynamicWhere("Id", "0").Single();
            Assert.IsTrue(person.FirstName == "Chris");
        }

        [TestMethod]
        public void DynamicWhere_PropertyIsGuid_FiltersCorrectly()
        {
            var people = DataContext.People();
            var filterGuid = ((Person) people.First()).TestGuid;
            var person = (Person)people.DynamicWhere("TestGuid", filterGuid).Single();
            Assert.IsTrue(person.FirstName == "Chris");
        }

        [TestMethod]
        public void DynamicWhere_PropertyIsNullableGuid_FiltersCorrectly()
        {
            var people = DataContext.People();
            var filterGuid = ((Person)people.First()).TestNullableGuid;
            var person = (Person)people.DynamicWhere("TestNullableGuid", filterGuid).Single();
            Assert.IsTrue(person.FirstName == "Chris");
        }

        [TestMethod]
        public void DynamicWhereGreaterThanEqualTo()
        {
            var people = DataContext.People();
            var firstPerson = (Person) people.First();
            firstPerson.BirthDate = DateTime.Now.AddDays(-2);
            var result = people.DynamicWhereGreaterThanEqualTo("BirthDate", DateTime.Now.AddDays(-1));
            var peopleCount = people.Count();
            var resultCount = result.Count();
            Assert.IsTrue(result.Count() == people.Count() - 1);
        }

        [TestMethod]
        public void DynamicWhereLessThanEqualTo()
        {
            var people = DataContext.People();
            var firstPerson = (Person)people.First();
            firstPerson.BirthDate = DateTime.Now.AddDays(-2);
            var result = people.DynamicWhereLessThanEqualTo("BirthDate", DateTime.Now.AddDays(-1));
            Assert.IsTrue(result.Count() == 1);
        }


    }
}
